package run.siyuan.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import run.siyuan.mapper.MemberAmountMapper;
import run.siyuan.model.CommissionSettlement;
import run.siyuan.model.Member;
import run.siyuan.model.MemberAmount;

import java.util.List;

/**
 * 佣金账单服务
 *
 * @author
 * @date 2021/03/19
 */
@Service
@Slf4j
public class CommissionSettlementExtendService {

    @Autowired
    private MemberAmountMapper memberAmountMapper;
    @Autowired
    private SettlementBalanceRecordService settlementBalanceRecordService;
    @Autowired
    private CommissionSettlementService commissionSettlementService;
    @Autowired
    private CommissionBillingService commissionBillingService;
    @Autowired
    private CommissionRecordService commissionRecordService;

    /**
     * 生成用户钱包
     *
     * @param member
     * @param commissionSettlements
     */
    @Transactional(rollbackFor = Exception.class)
    public void generateMemberAmount(Member member, List<CommissionSettlement> commissionSettlements) {

            MemberAmount memberAmount = memberAmountMapper.selectOne(Wrappers.lambdaQuery(MemberAmount.class).eq(MemberAmount::getMemberId,member.getId()));

            List<Long> commissionSettlementIds = settlementBalanceRecordService.saveSettlementBalanceRecord(memberAmount, commissionSettlements);

            commissionSettlementService.updateCommissionSetlements(commissionSettlementIds);

            List<Long> commissionBillingIds = commissionBillingService.updateCommissionBillings(commissionSettlements);
//            log.info("commissionBillingIds:{}",commissionBillingIds.toString());
            commissionRecordService.updateCommissionRecords(commissionBillingIds);

    }


}
